package com.hzz.gulimall.auth.config;

import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.common.utils.R;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 胡忠智
 * @date 2022/1/28-1:16
 * @Description TODO
 */
@Configuration
public class JiaJiamallSentinelConfig implements UrlBlockHandler {

    @Override
    public void blocked(HttpServletRequest request,
                        HttpServletResponse response,
                        BlockException ex) throws IOException {
        R r = R.error(BizCodeEnum.TO_MANY_REQUEST.getCode(),BizCodeEnum.TO_MANY_REQUEST.getMsg());
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(r)+"servlet用法");
    }
}
